/*
 * Created on Feb 29, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package ModuleBuilder.UI.tree;

import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.BorderFactory; 
import javax.swing.border.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import ModuleBuilder.*;
import ModuleBuilder.model.*;	
import ModuleBuilder.UI.*;
import java.beans.*;


/**
 * @author Milton  Soong
 *
 * Use by the OBEditor to represent a specific instance of a attribute 
 */
public class AttribValuePanel extends JPanel {
	private static AttribValuePanel loc = new AttribValuePanel(null);
	protected String name;
	
	public AttribValuePanel( String nameIn ) {
		Dimension d = new Dimension(100, 40);
		this.setMinimumSize(d);
		this.setPreferredSize(d);
		this.setMaximumSize(d);
		// this.setBorder(BorderFactory.createEtchedBorder());
		this.setBorder(BorderFactory.createLoweredBevelBorder());
		name = nameIn;
	}
	
	public static AttribValuePanel create(UnitProfileAttribute in) {	
		AttribValuePanel res=null;
		String nameIn = in.getName();
		Object obj = in.getVal();
		
		if (obj instanceof Integer) {
			res = loc.new NumAttribValuePanel(nameIn, (Integer)obj);
		} else if ( obj instanceof String ) {
			res = loc.new StringAttribValuePanel(nameIn, (String)obj);
		} else if ( obj instanceof RangeUnitProfileAttribute ) {
			res = loc.new RangeAttribValuePanel(nameIn, (RangeUnitProfileAttribute)obj);
		}
		return (res);
	}
	
	// this one will contain a JSpinner that contains a number from 0 to 999
	public class NumAttribValuePanel extends AttribValuePanel {
		
		public NumAttribValuePanel(String nameIn, Integer in) {
			super(nameIn);
			JLabel lab = new JLabel(name + ":" + in.toString());
			this.add(lab);
		}
	}
	
	public class StringAttribValuePanel  extends AttribValuePanel {
		
		
		public StringAttribValuePanel(String nameIn, String in) {
			super(nameIn);
			JLabel lab = new JLabel(name + ":" + in); 
			this.add(lab);
		}
	}
	
	public class RangeAttribValuePanel  extends AttribValuePanel {
		SpinnerNumberModel model;
		
		public  RangeAttribValuePanel(String nameIn, RangeUnitProfileAttribute in) {
			super(nameIn);
			JLabel lab = new JLabel(name + ":"); 
			this.add(lab);
			// use the max value as the init value in this case
			model = new SpinnerNumberModel(in.getMaxVal(), in.getMinVal(), in.getMaxVal(), in.getStepVal());
			JSpinner spin = new JSpinner(model);
			this.add(spin);
		}
		
		// Get object will return the selected value ONLY!
		public Object getObject() {
			Object res = model.getNumber();
			return (res);
		}
		
		public void setObject( Object valIn ) {
			model.setValue(valIn);
			return;
		}
	}

	/**
	 * @return
	 */
	public String getName() {
		return name;
	}

}
